import { _decorator, Component, Node, tween, v3, Vec3 } from 'cc';
import { EffectManager } from '../game/EffectManager';
import { Enemy } from '../actor/Enemy';
const { ccclass, property } = _decorator;

@ccclass('Projectile')
export class Projectile extends Component {
    private exploedPos: Vec3 = v3();

    /** 子弹飞向 */
    public setAttackTarget(flyTime: number, target: Enemy) {
        if (target) {
            this.exploedPos = target.node.getWorldPosition().clone();
            tween(this.node)
                .to(flyTime, { worldPosition: this.exploedPos })
                .call(() => {
                    this.onExplode();
                })
                .start()
        }
    }

    private onExplode() {
        EffectManager.Instance.playExplodeEffect(this.exploedPos);
        this.scheduleOnce(() => {
            this.node.destroy();
        })
    }
}